/**
 * 
 */
package com.hs.chd.helper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

/**
 * @author Administrator
 *
 */
public class SQLHelper {
    private static String COL_PERFIX ="sel";
    private static String COL_VALUE = "value";
    private static String COL_LOGICAL ="logical";
    private static String COL_SPECIAL = "special";
    /**
     * 初始化页面检索参数
     * @param request
     * @param paramnamestr
     * @return
     */
	public  Map initParams(HttpServletRequest request,String paramnamestr){
		//List plist = new ArrayList();
		Map paramMap = new HashMap();
		if(StringUtils.isNotEmpty(paramnamestr)){
	       	 String[] paramarr = paramnamestr.split("\\$");
	       	 for(int i=0;i<paramarr.length;i++){
	       		 SearcherObj so = new SearcherObj();
	       		 String temp = paramarr[i];
	       		 String colvalue = request.getParameter(getColname(temp,COL_VALUE));
	       		 String collogical = request.getParameter(getColname(temp,COL_LOGICAL));
	       		 String colspecial = request.getParameter(getColname(temp,COL_SPECIAL));
	       		 String colname = request.getParameter(temp);
	       		 
	       		 so.setCol_selname(colname);
	       		 so.setCol_value(colvalue);
	       		 so.setCol_special(colspecial);
	       		 so.setCol_logical(collogical==null?"AND":collogical);
	       		// plist.add(so);
	       		 
	       		 if(null == paramMap.get(colname)){
	       			List singleList = new ArrayList();
	       			singleList.add(so);
	       			paramMap.put(colname, singleList);
	       		 }else{
	       			List singleList = (List)paramMap.get(colname);
	       			singleList.add(so);
	       		 } 
	       	 }
        }
		return paramMap; 
	}
	private static String getColname(String colname,String replacestr){
		if(StringUtils.isNotEmpty(colname)){
			return colname.replace(COL_PERFIX, replacestr);
		}
		return colname;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
        String t = "$$txt_1_sel$$txt_2_sel$$txt_3_sel";
        
       System.out.println(t.split("\\$$",2)==null?"0":t.split("\\$$",2).length);
	}
	public String processSql(Map pmap){
		System.out.println("pmap:" +pmap);
		Set keyset = pmap.keySet();
		StringBuffer  andstr = new StringBuffer("");
		StringBuffer  orstr = new StringBuffer("");
		StringBuffer  fullstr = new StringBuffer("");	
		Iterator iter = keyset.iterator();
		int k = 0;
		while(iter.hasNext()){
			 
			String key = (String)iter.next();
			List plist = (List)pmap.get(key);
			System.out.println("key:" + key);
			System.out.println("key:" + plist.size());
			if(plist.size() == 1){
				SearcherObj obj =  (SearcherObj)plist.get(0);
				System.out.println("obj:" + obj.getCol_logical());
				if("AND".equalsIgnoreCase(obj.getCol_logical())){
					if(k>0)
						andstr.append(" AND ");
					andstr.append(obj.getSqlstr());
				}else if("OR".equalsIgnoreCase(obj.getCol_logical())){
					if(k>0)
						orstr.append(" OR ");
					orstr.append(obj.getSqlstr());
				}
			}else{
				
				andstr.append(getOneColstr(key,plist)); 
			}
			k++;
		}
		System.out.println("andstr:" + andstr);
		boolean andisnotnull = StringUtils.isNotEmpty(andstr.toString());
		boolean orisnotnull = StringUtils.isNotEmpty(orstr.toString());
		if(andisnotnull && orisnotnull){
			fullstr.append("((");
			fullstr.append(andstr);
			fullstr.append(") OR ");
			fullstr.append(orstr); 
			fullstr.append(")");
		}else  if(andisnotnull && !orisnotnull){
			fullstr.append("(");
			fullstr.append(andstr);
			fullstr.append(")"); 
		}else  if(!andisnotnull && orisnotnull){
			fullstr.append("(");
			fullstr.append(orstr);
			fullstr.append(")");  
		}else  if(!andisnotnull && !orisnotnull){
			 
		} 
		return fullstr.toString();
	}
	private String getOneColstr(String key ,List Plist){
 		StringBuffer andstr = new StringBuffer("");
		StringBuffer orstr = new StringBuffer("");	
		StringBuffer allstr = new StringBuffer("");	
		for(int i =0;i<Plist.size();i++){
			SearcherObj obj =  (SearcherObj)Plist.get(i);
			System.out.println("obj:" + obj.getCol_logical());
			if("AND".equalsIgnoreCase(obj.getCol_logical())){ 
				if(i > 0){
					andstr.append(" AND ");
				} 
				andstr.append(obj.getSqlstr());
			}else if("OR".equalsIgnoreCase(obj.getCol_logical())){ 
				if(i > 0){
					andstr.append(" OR ");
				}  
				orstr.append(obj.getSqlstr());
			}
		} 
		boolean andisnotnull = StringUtils.isNotEmpty(andstr.toString());
		boolean orisnotnull = StringUtils.isNotEmpty(orstr.toString());
		if(andisnotnull && orisnotnull){
			allstr.append(" AND ( ");
			allstr.append(andstr);  
			allstr.append(orstr); 
			allstr.append(")");
		}else  if(andisnotnull && !orisnotnull){ 
			allstr.append(" AND ");
			allstr.append(andstr); 
		}else  if(!andisnotnull && orisnotnull){
			allstr.append(" AND ");
			allstr.append("(");
			allstr.append(orstr);
			allstr.append(")");  
		}else  if(!andisnotnull && !orisnotnull){
			 
		}   
		return allstr.toString();
	}
	 

}
